from challenges.challenge import Challenge
from common import BaseRequestHandler, STARTING_MONEY
from datetime import datetime
from google.appengine.api import users
from google.appengine.api.users import User
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from levels.level1 import Level1Page
from levels.level2 import Level2Page
from levels.level3 import Level3Page
from levels.level4 import Level4Page
from lib.dbobjects import Profiler

##@details This is the main class that deals with profiles
class MainPage(BaseRequestHandler):
    
    def get(self):
        if users.is_current_user_admin():
            self.redirect('/admin')
        else:
            self.display('main.html', users.get_current_user())
        
    def post(self):
        profilers_query = Profiler.all().filter("user =",
                users.get_current_user()).order('-modified')
        profiler = profilers_query.get()
        if profiler is not None:
            print "If you already have a profile, how did you get here?"
            raise
        profiler = Profiler(user=users.get_current_user(),
                nickname=self.request.get('nickname'), money=STARTING_MONEY)
        philanthropist_query = Profiler.all().filter("user =",
                User("Philanthropist")).order('-modified')
        philanthropist = philanthropist_query.get()
        if philanthropist is None:
            philanthropist = Profiler(user = User("Philanthropist"),
                    nickname = "Teddy Rist", money = 0)
        philanthropist.money -= STARTING_MONEY
        profiler.put()
        philanthropist.put()
        self.redirect('/')
        
class AdminPage(BaseRequestHandler):
    def get(self):
        if users.is_current_user_admin():
            self.display()
        else:
            self.redirect('/')
            
    def post(self):
        pass
    
    def display(self):
        # Get all the profilers sorted by date last modified
        profilers_query = Profiler.all().order('-modified')
        profilers = profilers_query.fetch(200)
        template_values = {'profilers': profilers}
        self.generate('admin.html', template_values)

class EditPage(BaseRequestHandler):
    def get(self):
        if not users.is_current_user_admin():
            self.redirect('/');
        else:
            self.display('edit.html', User(self.request.get('username')))
        
    def post(self):
        profilers_query = Profiler.all().filter("user =",
                User(self.request.get('username'))).order('-modified')
        profiler = profilers_query.get()
        profiler.nickname = self.request.get('nickname')
        profiler.modified = datetime.now()
        old_money = profiler.money
        new_money = int(self.request.get('money'))
        philanthropist_query = Profiler.all().filter("user =",
                User("Philanthropist")).order('-modified')
        philanthropist = philanthropist_query.get()
        profiler.money = new_money
        philanthropist.money += old_money
        philanthropist.money -= new_money
        profiler.put()
        philanthropist.put()
        self.redirect('/admin')
    
class RefillPage(BaseRequestHandler):

    def get(self):
        self.display('refill.html', users.get_current_user())
        
    def post(self):
        profilers_query = Profiler.all().filter("user =",
                users.get_current_user()).order('-modified')
        profiler = profilers_query.get()
        if profiler.money == 0:
            profiler.modified = datetime.now()
            profiler.excuse = self.request.get('excuse')
            philanthropist_query = Profiler.all().filter("user =",
                    User("Philanthropist")).order('-modified')
            philanthropist = philanthropist_query.get()
            profiler.times_busted += 1
            new_money = STARTING_MONEY - (profiler.times_busted % 500)
            philanthropist.money -= new_money
            profiler.money += new_money
            philanthropist.put()
            profiler.put()
        self.redirect('/')
        

application = webapp.WSGIApplication([('/', MainPage),
                                      ('/admin', AdminPage),
                                      ('/edit_user', EditPage),
                                      ('/refill', RefillPage),
                                      ('/level1', Level1Page),
                                      ('/level2', Level2Page),
                                      ('/level3', Level3Page),
                                      ('/level4', Level4Page),
                                      ('/challenge', Challenge)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()